home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1752 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: news.microsoft.com!news
  2. From: a-cnadc@microsoft.com (Dann Corbit)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help, best way to compare doubles
  5. Date: 16 Jan 1996 17:36:37 GMT
  6. Organization: Microsoft Corporation
  7. Message-ID: <4dgnn5$em@news.microsoft.com>
  8. References: <4da169$mm4@mercury.IntNet.net>
  9. NNTP-Posting-Host: 157.57.171.202
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.93.14
  12.  
  13. In article <4da169$mm4@mercury.IntNet.net>, jtomich@IntNet.net says...
  14. >
  15. >Looking for a way to compare doubles for equality.
  16. >
  17. A very dangerous practice, as computing the same result two different 
  18. ways may not have exactly the same result.  Sometimes a test such as:
  19.   abs( double1 - double2 ) < dEpsilon 
  20. is used, but this technique is not always valid.  Suppose, for instance,
  21. that double1 and double2 are enormous numbers.  Though they might differ
  22. only in the least significant bit, the difference could still be huge.
  23. Sometimes a relative error is calculated, such as:
  24.      abs( double1 - double2 )/max( double1, double2 ) < dEpsilon 
  25. but that has a weakness if double1 and double2 are vastly different values.
  26. The best solution will vary greatly, depending on what you actually want
  27. to accomplish.
  28.  
  29. The abridged FAQ says:
  30.  
  31. 14.5:   What's a good way to check for "close enough" floating-point
  32.         equality?
  33.  
  34. A:      The best way is to use an accuracy threshold which is relative
  35.         to the magnitude of the numbers being compared.
  36.  
  37. and in general, that's pretty good advice.  But in any case, you should
  38. do some careful analysis to make sure that the code accomplishes what 
  39. you want it to.
  40. -- 
  41. The opinions expressed in this message are my own personal views 
  42. and do not reflect the official views of Microsoft Corporation.
  43.  
  44.